package cn.itcast.demo;

import cn.itcast.demo.pojo.Heima63;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.io.IOException;

public class SolrJCreateIndex {


    /**
     * 要操作solr，必须先要访问到solr
     *
     *
     */
    @Test
    public void testCreateIndex() throws IOException, SolrServerException {
        //访问solrServer
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //创建文档
        SolrInputDocument document = new SolrInputDocument();

        document.addField("id","10086");
        document.addField("title","我是用solrJ来测试的");
        document.addField("price","123456789");

        //把solr文档添加给solr服务器
        solrServer.add(document);

        solrServer.commit();

    }



    @Test
    public void testCreateIndexWithBean() throws IOException, SolrServerException {
        //访问solrServer
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //创建文档
        Heima63 heima63 = new Heima63();
        heima63.setId("12345");
        heima63.setTitle("今天学习solrcloud");
        heima63.setPrice(123465L);

        //把solr文档添加给solr服务器
        solrServer.addBean(heima63);

        solrServer.commit();

    }


    //修改的api和新增的api不变，id是唯一，只要保证这个id出现过，就会删除对应的文档然后重新添加
    @Test
    public void testUpdateIndex() throws IOException, SolrServerException {
        //访问solrServer
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //创建文档
        SolrInputDocument document = new SolrInputDocument();

        document.addField("id","10086");
        document.addField("title","我是用solrJ来测试修改的的");
        document.addField("price","654321");

        //把solr文档添加给solr服务器
        solrServer.add(document);

        solrServer.commit();

    }
}
